<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        img{
            /* 清空垂直的间隙 */
            vertical-align: top;
        }
        .box{
            position: relative;
            margin: 100px;
            width: 350px;
            height: 350px;
            border: 1px solid #000000;
        }
        .box>.small>.mask{
            display: none;
            position: absolute;
            left: 0;
            top: 0;
            width: 100px;
            height: 100px;
            background-color: rgba(255,255,0,.3);
            /* background-color:red; */
            cursor: move;
        }
        .box>.big{
            /* display: b; */
            display: none;
            position: absolute;
            top: 0;
            left: 360px;
            width: 400px;
            height: 400px;
            border: 1px solid #000000;
            overflow: hidden;
        }
        .bigImg{
            position: absolute;
            left: 0;
            top: 0;
            
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="small">
            <img src="./001.jpg" alt="鼠标图片">
            <!-- 遮罩层 -->
            <span class="mask"></span>
        </div>
        <div class="big">
            <img src="./0001.jpg" alt="" class="bigImg">
        </div>
    </div>
</body>
<script>
    // 获取遮罩层
    var box = document.querySelector('.box')
    var small = document.querySelector('.small')
    var mask = document.querySelector('.mask')
    var big = document.querySelector('.big')
    var bigImg = document.querySelector('.bigImg')
    small.onmouseover = function(){
        // console.log('aaa');
        mask.style.display = 'block'
        big.style.display = 'block'
    }
    small.onmouseout = function(){
        // console.log('aaa');
        mask.style.display = 'none'
        big.style.display = 'none'
    }
    small.onmousemove = function(event){
        event = event || window.event
        var x = event.clientX - box.offsetLeft - mask.offsetWidth/2
        var y = event.clientY - box.offsetTop - mask.offsetHeight/2

        if(x<0){
            x=0
        }else if(x> box.offsetWidth - mask.offsetWidth){
            x = box.offsetWidth - mask.offsetWidth
        }

        if(y<0){
            y=0
        }else if(y>box.offsetHeight - mask.offsetHeight){
            y=box.offsetHeight - mask.offsetHeight
        }
        
        mask.style.left = x + 'px'
        mask.style.top = y + 'px'

        bigImg.style.left = - x * (bigImg.offsetWidth/small.offsetWidth) + 'px'
        bigImg.style.top = - y  * (bigImg.offsetHeight/small.offsetHeight) + 'px'
    }
</script>
</html>